1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10
11 //Nghị code all
12
13 namespace SoftQuanLyNhaHang.Views
14 {
15 public partial class uctDonViTinh : UserControl
16 {
17 public uctDonViTinh()
18 {
19 InitializeComponent();
20 }
21
22 //khởi tạo gọi trong menuscript
23 public static uctDonViTinh uctDVT = new uctDonViTinh();
24 int flag = 0;
25 int _idDonViTinh = 0;
26 string _maDonViTinh = "";
27 string _tenDonViTinh = "";
28 int _viTri = 0;
29 int _trangThai = 1;
30 DataTable dt = new DataTable();
31
32 int _pageSize = 15;
33 int _pageIndex = 1;
34 int _tongSoBanGhi = 0;
35 int _stt = 0;
36 int _tongSoTrang = 1;
37
38 public void HienThiDanhSachDonViTinh()
39 {
40 dgvDSDonViTinh.Rows.Clear();
41 //dt = Controllers.DonViTinhCtrl.FillDataSetDonViTinh_GetDanhSach().Tables[0];
42 dt = Controllers.DonViTinhCtrl.FillDataSet_spTblDonViTinh_Search(_pageSize, _pageIndex).Tables[0];
43
44 if (dt.Rows.Count > 0)
45 {
46
47
48 string tthai;
49
50 foreach (DataRow item in dt.Rows)
51 {
52
53 _stt += 1;
54 int n = dgvDSDonViTinh.Rows.Add();
55 dgvDSDonViTinh.Rows[n].Cells[0].Value = item["IdDonViTinh"].ToString();
56 dgvDSDonViTinh.Rows[n].Cells[1].Value = _stt.ToString();
57 dgvDSDonViTinh.Rows[n].Cells[2].Value = item["TenDonViTinh"].ToString();
58
59 if (item["TrangThai"].ToString() == "1")
60 {
61 tthai = "Sử dụng";
62 }
63 else
64 {
65 tthai = "không";
66 }
67
68 dgvDSDonViTinh.Rows[n].Cells[3].Value = tthai;
69 dgvDSDonViTinh.Rows[n].Cells[4].Value = "false";
70 dgvDSDonViTinh.Rows[n].Cells[5].Value = item["MaDonViTinh"].ToString();
71 dgvDSDonViTinh.Rows[n].Cells[6].Value = item["TongSoBanGhi"].ToString();
72
73 }
74 _tongSoBanGhi = Int32.Parse(dgvDSDonViTinh.Rows[0].Cells[6].Value.ToString());
75 HienThiPhanTrang( _tongSoBanGhi, _pageSize, _pageIndex);
76
77 //_tongSoTrang = _tongSoBanGhi /
78
79 }
80
81
82 //dgvDSDonViTinh.DataSource = Models.DonViTinhMod.FillDataSetDonViTinh().Tables[0];
83 //dgvDSDonViTinh.Dock = DockStyle.Fill;
84 //dgvDSDonViTinh.RowHeadersVisible = false;
85 //dgvDSDonViTinh.BorderStyle = BorderStyle.Fixed3D;
86 }
87
88 void clearData()
89 {
90 //txtIdDonViTinh.Text = Models.connection.ExcuteScalar(String.Format("select IdDonViTinh= dbo.fcgetIdNhanVien()"));
91 //txtMaDonVi.Text = "";
92 //txtTenDonViTinh.Text = "";
93 //loadcontrol();
94 _idDonViTinh = 0;
95 txtMaDonVi.Text = "";
96 txtTenDonViTinh.Text = "";
97 rdoSuDung.Checked = true;
98 }
99
100 void dis_end(bool e)
101 {
102 txtMaDonVi.Enabled = e;
103 txtTenDonViTinh.Enabled = e;
104 }
105
106 private void loadcontrol()
107 {
108 cmbSoDongHienThi.Items.Clear();
109 cmbSoDongHienThi.Items.Add("15");
110 cmbSoDongHienThi.Items.Add("30");
111 cmbSoDongHienThi.Items.Add("50");
112 cmbSoDongHienThi.Items.Add("100");
113 cmbSoDongHienThi.SelectedItem = "15";
114 }
115
116 private void btnThemDonViTinh_Click(object sender, EventArgs e)
117 {
118 //flag = 0;
119 //clearData();
120 //dis_end(true);
121 if (_idDonViTinh > 0)
122 {
123 MessageBox.Show("Tên đơn vị tính đã tồn tại trong hệ thống!");
124 return;
125 }
126
127 try
128 {
129 _maDonViTinh = txtMaDonVi.Text.Trim();
130 }
131 catch { }
132
133
134 try
135 {
136 _tenDonViTinh = txtTenDonViTinh.Text.Trim();
137 }
138 catch { }
139
140 try
141 {
142 if (rdoSuDung.Checked == true)
143 {
144 _trangThai = 1;
145 }
146 else
147 {
148 _trangThai = 0;
149 }
150 }
151 catch
152 {
153
154 }
155
156 if (_maDonViTinh == "" || _tenDonViTinh == "")
157 MessageBox.Show("Hãy nhập đầy đủ thông tin");
158 else
159 {
160 int i = 0;
161 i = Controllers.DonViTinhCtrl.InsertDonViTinh(_maDonViTinh, _tenDonViTinh, _viTri, _trangThai);
162 if (i > 0)
163 {
164 MessageBox.Show("Thêm mới thành công");
165 HienThiDanhSachDonViTinh();
166 this.clearData();
167 }
168 else
169 {
170 MessageBox.Show("Thêm mới không thành công");
171 }
172
173 }
174
175
176
177 }
178
179 private void txtSuaDonViTinh_Click(object sender, EventArgs e)
180 {
181
182 try
183 {
184 _maDonViTinh = txtMaDonVi.Text.Trim();
185 }
186 catch { }
187
188
189 try
190 {
191 _tenDonViTinh = txtTenDonViTinh.Text.Trim();
192 }
193 catch { }
194
195 try
196 {
197 if (rdoSuDung.Checked == true)
198 {
199 _trangThai = 1;
200 }
201 else
202 {
203 _trangThai = 0;
204 }
205 }
206 catch
207 {
208
209 }
210
211 if (_maDonViTinh == "" || _tenDonViTinh == "")
212 MessageBox.Show("Hãy nhập đầy đủ thông tin");
213 else
214 {
215 int i = 0;
216 i = Controllers.DonViTinhCtrl.UpdateDonViTinh(_idDonViTinh, _maDonViTinh, _tenDonViTinh, _viTri, _trangThai);
217 if (i > 0)
218 {
219 MessageBox.Show(" Sửa thành công");
220 HienThiDanhSachDonViTinh();
221 this.clearData();
222 }
223 else
224 {
225 MessageBox.Show("Sửa không thành công");
226 }
227 }
228
229
230 //flag = 1;
231 //dis_end(true);
232 /*
233 foreach (DataGridViewRow dataRow in dgvDSDonViTinh.Rows)
234 {
235 if (bool.Parse(dataRow.Cells[4].Value.ToString()))
236 {
237 txtTenDonViTinh.Text = dataRow.Cells[2].Value.ToString();
238 txtMaDonVi.Text = dataRow.Cells[5].Value.ToString();
239 if (dataRow.Cells[3].Value.ToString() == "Sử dụng")
240 {
241 rdoSuDung.Checked = true;
242 }
243 else
244 {
245 rdoKhong.Checked = true;
246 }
247
248 _idDonViTinh = Int32.Parse( dataRow.Cells[0].Value.ToString());
249 return;
250 }
251 }//end foreach
252
253 */
254 }
255
256 private void btnXoaDonViTinh_Click(object sender, EventArgs e)
257 {
258 if (string.IsNullOrEmpty(txtMaDonVi.Text.Trim()) && string.IsNullOrEmpty(txtTenDonViTinh.Text.Trim()))
259 {
260 MessageBox.Show("Bạn chưa chọn đơn vị tính để xóa!");
261 return;
262 }
263 foreach (DataGridViewRow dataRow in dgvDSDonViTinh.Rows)
264 {
265 if (bool.Parse(dataRow.Cells[4].Value.ToString()))
266 {
267 DialogResult dr = MessageBox.Show("Bạn có chắc chắn xóa?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
268 if (dr == DialogResult.Yes)
269 {
270 int i = 0;
271 i = Controllers.DonViTinhCtrl.DeleteDonViTinh(Int32.Parse(dataRow.Cells[0].Value.ToString()));
272
273 if (i > 0)
274 {
275 MessageBox.Show(" Xóa thành công");
276 HienThiDanhSachDonViTinh();
277 }
278 else
279 {
280 MessageBox.Show("Xóa không thành công");
281 }
282 }
283 else
284 {
285 return;
286 }
287 }
288 }//end foreach
289 this.clearData();
290 }
291
292 private void btnLuuDonViTinh_Click(object sender, EventArgs e)
293 {
294
295 try
296 {
297 _maDonViTinh = txtMaDonVi.Text.Trim();
298 }
299 catch { }
300
301
302 try
303 {
304 _tenDonViTinh = txtTenDonViTinh.Text.Trim();
305 }
306 catch { }
307
308
309
310 if (flag == 0)
311 {
312 if ( _maDonViTinh == "" || _tenDonViTinh == "")
313 MessageBox.Show("Hãy nhập đầy đủ thông tin");
314 else
315 {
316 int i = 0;
317 i = Controllers.DonViTinhCtrl.InsertDonViTinh(_maDonViTinh, _tenDonViTinh, _viTri, _trangThai);
318 if (i > 0)
319 {
320 MessageBox.Show("Thêm mới thành công");
321 HienThiDanhSachDonViTinh();
322 }
323 else
324 MessageBox.Show("Thêm mới không thành công");
325 }
326 }
327 else
328 {
329 int i = 0;
330 i = Controllers.DonViTinhCtrl.UpdateDonViTinh(_idDonViTinh, _maDonViTinh, _tenDonViTinh, _viTri, _trangThai);
331 if (i > 0)
332 {
333 MessageBox.Show(" Sửa thành công");
334 HienThiDanhSachDonViTinh();
335 //uctNhanVien_Load(sender, e);
336 }
337 else
338 {
339
340 MessageBox.Show("Sửa không thành công");
341 }
342
343 }
344
345 //uctDonViTinh_Load(sender, e);
346
347 }
348
349 private void uctDonViTinh_Load(object sender, EventArgs e)
350 {
351 this.loadcontrol();
352 //dis_end(false);
353 HienThiDanhSachDonViTinh();
354
355
356
357 if (_tongSoBanGhi <= _pageSize)
358 {
359 btnSau.Visible = false;
360 btnTiep.Visible = false;
361 pnlPhanTrang.Visible = false;
362 }
363 //bingding();
364 }
365
366
367 void bingding()
368 {
369 //txtIdDonViTinh.DataBindings.Clear();
370 //txtIdDonViTinh.DataBindings.Add("Text", dgvDSDonViTinh.DataSource, "IdDonViTinh");
371 txtMaDonVi.DataBindings.Clear();
372 txtMaDonVi.DataBindings.Add("Text", dgvDSDonViTinh.DataSource, "MaDonViTinh");
373 txtTenDonViTinh.DataBindings.Clear();
374 txtTenDonViTinh.DataBindings.Add("Text", dgvDSDonViTinh.DataSource, "TenDonViTinh");
375 }
376
377
378 private void dgvDSDonViTinh_CellContentClick(object sender, DataGridViewCellEventArgs e)
379 {
380
381 }
382
383 private void btnHuy_Click(object sender, EventArgs e)
384 {
385 this.HienThiDanhSachDonViTinh();
386 this.clearData();
387
388 }
389
390 private void dgvDSDonViTinh_CellClick(object sender, DataGridViewCellEventArgs e)
391 {
392 if (e.ColumnIndex == 4)
393 {
394 txtMaDonVi.Text = dgvDSDonViTinh.Rows[e.RowIndex].Cells[5].Value.ToString();
395 txtTenDonViTinh.Text = dgvDSDonViTinh.Rows[e.RowIndex].Cells[2].Value.ToString();
396 _idDonViTinh = Int32.Parse(dgvDSDonViTinh.Rows[e.RowIndex].Cells[0].Value.ToString());
397
398 if (dgvDSDonViTinh.Rows[e.RowIndex].Cells[3].Value.ToString() == "Sử dụng")
399 {
400 rdoSuDung.Checked = true;
401 }
402 else
403 {
404 rdoKhong.Checked = true;
405 }
406 }
407 }
408
409 private void HienThiPhanTrang(int tongSoBanGhi, int pageSize, int pageIndex)
410 {
411 if (tongSoBanGhi <= pageSize)
412 {
413 pnlPhanTrang.Visible = false;
414 }
415 else
416 {
417
418 }
419 }
420
421 private void btnTiep_Click(object sender, EventArgs e)
422 {
423 if (_tongSoBanGhi > (_pageSize * _pageIndex))
424 {
425 _pageIndex = _pageIndex + 1;
426 this.HienThiDanhSachDonViTinh();
427 /*
428 for (int x = 0; x <= 4; x++)
429 {
430 switch (x)
431 {
432 case 1:
433 btn1.Text = x.ToString();
434 break;
435 case 2:
436 btn1.Text = x.ToString();
437 break;
438 case 3:
439 btn1.Text = x.ToString();
440 break;
441 case 4:
442 btn1.Text = x.ToString();
443 break;
444 default:
445 btn1.Text = x.ToString();
446 break;
447 }
448
449 }
450 */
451 }
452 else
453 {
454 return;
455 }
456 }
457
458 private void btnSau_Click(object sender, EventArgs e)
459 {
460 if ((_pageSize * _pageIndex) > _pageSize)
461 {
462 _pageIndex = _pageIndex - 1;
463 _stt = _pageSize * _pageIndex - _pageSize;
464 this.HienThiDanhSachDonViTinh();
465
466
467 }
468 else
469 {
470 _pageIndex = 1;
471 }
472 }
473
474 private void cmbSoDongHienThi_SelectedIndexChanged(object sender, EventArgs e)
475 {
476 }
477
478 private void cmbSoDongHienThi_TextChanged(object sender, EventArgs e)
479 {
480 }
481
482 //Thay đổi combobox hiển thị số bản ghi mới
483 private void cmbSoDongHienThi_SelectionChangeCommitted(object sender, EventArgs e)
484 {
485 _pageSize = Int32.Parse(cmbSoDongHienThi.SelectedItem.ToString());
486 _stt = 0;
487 this.HienThiDanhSachDonViTinh();
488 }
489 }
490 }